﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

using Glodon.Gdmp.DB;
using System.Collections.Generic;

namespace Glodon.Gdmp.SDK.Samples.IFCExport
{
    /// <summary>
    /// IFC创建工具类。
    /// </summary>
    public static class IFCUtils
    {
        //IFC工程
        private static IFCEntity _entityProject;

        //IFC所有者信息
        public static IFCEntity IFCOwnerHistory;
        
        //IFC模型信息
        public static IFCEntity BuildingStory;

        /// <summary>
        /// IFC导出初始化。
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        public static void Initialize(this IFCBuildingModel model)
        {
            _entityProject = model.CreateEntity("IFCPROJECT");
            //创建所有者信息。
            IFCOwnerHistory = CreateIfcOwnerHistory(model);
            IFCEntity ifcGeometricRepresentationContext = CreateGeometricRepresentationContext(model);
            CreateDefaultSiteAndBuildingStorey(model);
            ExportIfcGeometricRepresentationSubContext(ifcGeometricRepresentationContext, model);
            SetIfcProjectConfig(model, _entityProject, ifcGeometricRepresentationContext);
        }
        /// <summary>
        /// 创建所有者信息。
        /// https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/schema/ifcutilityresource/lexical/ifcownerhistory.htm
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity CreateIfcOwnerHistory(IFCBuildingModel model)
        {
            var ifcOwnerHistory = model.CreateEntity("IFCOWNERHISTORY");

            List<IFCObject> attributes = new List<IFCObject>();
            //设置所有者用户或组织，此处为null。
            attributes.Add(null);
            //设置所有者应用。
            var application = model.CreateEntity("IFCAPPLICATION");
            attributes.Add(application);
            //设置状态。
            var stateEnum = IFCValueObject.Create("READWRITE", "IFCSTATEENUM");
            attributes.Add(stateEnum);
            //设置变更行为。
            var ifcChangeActionEnum = IFCValueObject.Create("NOCHANGE", "IFCCHANGEACTIONENUM");
            attributes.Add(ifcChangeActionEnum);
            //设置最新修改日期。
            var lastModifiedDate = IFCValueObject.Create(20240330, "IFCTIMESTAMP");
            attributes.Add(lastModifiedDate);
            //设置最新修改者，此处可为null。
            attributes.Add(null);
            //设置最新修改应用。
            attributes.Add(application);
            //设置UTC的创建日期。
            var creationDate = IFCValueObject.Create(20240330, "IFCTIMESTAMP");
            attributes.Add(creationDate);

            ifcOwnerHistory.SetAttributes(attributes);
            return ifcOwnerHistory;
        }

        /// <summary>
        /// 创建几何上下文信息。
        /// https://standards.buildingsmart.org/IFC/RELEASE/IFC4/ADD1/HTML/schema/ifcrepresentationresource/lexical/ifcgeometricrepresentationcontext.htm
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity CreateGeometricRepresentationContext(IFCBuildingModel model)
        {
            var context = model.CreateEntity("IFCGEOMETRICREPRESENTATIONCONTEXT");
            List<IFCObject> attributes = new List<IFCObject>();

            //设置标识符。
            var identifier = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(identifier);
            //设置类型。
            var contextType = IFCValueObject.Create("Model", "IFCLABEL");
            attributes.Add(contextType);
            //设置维度。
            var coordinateSpaceDimension = IFCValueObject.Create(3, "IFCDIMENSIONCOUNT");
            attributes.Add(coordinateSpaceDimension);
            //设置精度。
            var precision = IFCValueObject.Create(1.0e-5, "IFCREAL");
            attributes.Add(precision);
            //设置世界坐标系。        
            attributes.Add(null);
            //设置正北。
            attributes.Add(null);

            context.SetAttributes(attributes);
            return context;
        }
        /// <summary>
        /// 创建默认IFCSite与BuildingStorey。
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        private static void CreateDefaultSiteAndBuildingStorey(IFCBuildingModel model)
        {
            var site = CreateIfcSite(model);
            var building = CreateIfcBuilding(model);
            BuildingStory = CreateIfcBuildingStorey(model);
            CreateIfcRelAggregation(model, _entityProject, new List<IFCObject>() { site });
            CreateIfcRelAggregation(model, site, new List<IFCObject>() { building });
            CreateIfcRelAggregation(model, building, new List<IFCObject>() { BuildingStory });
        }

        /// <summary>
        /// 创建默认IFCSite。
        /// https://standards.buildingsmart.org/MVD/RELEASE/IFC4/ADD2_TC1/RV1_2/HTML/schema/ifcproductextension/lexical/ifcsite.htm
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity CreateIfcSite(IFCBuildingModel model)
        {
            var site = model.CreateEntity("IFCSITE");

            List<IFCObject> attributes = new List<IFCObject>();
            //设置全局唯一标识符。
            var uniqueId = IFCValueObject.CreateIfcGlobalUniqueId();
            attributes.Add(uniqueId);
            //设置所有者信息。
            attributes.Add(IFCOwnerHistory);
            //设置名称。
            var name = IFCValueObject.Create("CustomName", "IFCLABEL");
            attributes.Add(name);
            //设置描述。
            var description = IFCValueObject.Create("CustomDescription", "IFCTEXT");
            attributes.Add(description);
            //设置类型。
            var objectType = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(objectType);
            //设置定位。
            var objectPlacement = model.CreateEntity("IFCLOCALPLACEMENT");
            List<double> origin = new List<double>() { 0, 0, 0 };
            Vector3d normal = new Vector3d(0, 0, 1);
            Vector3d direction = new Vector3d(1, 0, 0);
            var placement = CreateIfcAxis2Placement3D(origin, normal, direction, model);
            IFCObject nullObject = null;
            objectPlacement.SetAttributes(new List<IFCObject>() { nullObject, placement });
            attributes.Add(objectPlacement);
            //设置形体。
            attributes.Add(null);
            //设置长名称。
            var longName = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(longName);
            //设置组件类型。
            var compositionType = IFCValueObject.Create("ELEMENT", "IFCELEMENTCOMPOSITIONENUM");
            attributes.Add(compositionType);
            //设置经度。
            var latitude = IFCValueObject.Create(new List<int> { }, "IFCCOMPOUNDPLANEANGLEMEASURE");
            attributes.Add(latitude);
            //设置纬度。
            var longitude = IFCValueObject.Create(new List<int> { }, "IFCCOMPOUNDPLANEANGLEMEASURE");
            attributes.Add(longitude);
            //设置标高。
            var elevation = IFCValueObject.Create(0.0, "IFCLENGTHMEASURE");
            attributes.Add(elevation);
            //设置场地编号。
            var landTitleNumber = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(landTitleNumber);
            //设置邮编。      
            attributes.Add(null);

            site.SetAttributes(attributes);
            return site;
        }

        /// <summary>
        /// 创建默认IFCBUILDING。
        /// https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/schema/ifcproductextension/lexical/ifcbuilding.htm
        /// </summary>
        /// <param name="model">IFC模型数据。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity CreateIfcBuilding(IFCBuildingModel model)
        {
            var building = model.CreateEntity("IFCBUILDING");

            List<IFCObject> attributes = new List<IFCObject>();
            //设置全局唯一标识符。
            var globallyUniqueId = IFCValueObject.CreateIfcGlobalUniqueId();
            attributes.Add(globallyUniqueId);
            //设置所有者信息。
            attributes.Add(IFCOwnerHistory);
            //设置名称。
            var name = IFCValueObject.Create("CustomName", "IFCLABEL");
            attributes.Add(name);
            //设置描述。
            var description = IFCValueObject.Create("CustomDescription", "IFCTEXT");
            attributes.Add(description);
            //设置类型。
            var objectType = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(objectType);
            //设置定位。
            var objectPlacement = model.CreateEntity("IFCLOCALPLACEMENT");
            List<double> origin = new List<double>() { 0, 0, 0 };
            Vector3d normal = new Vector3d(0, 0, 1);
            Vector3d direction = new Vector3d(1, 0, 0);
            var placement = CreateIfcAxis2Placement3D(origin, normal, direction, model);
            IFCObject nullObject = null;
            objectPlacement.SetAttributes(new List<IFCObject>() { nullObject, placement });
            attributes.Add(objectPlacement);
            //设置形体。
            attributes.Add(null);
            //设置长名称。
            var longName = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(longName);
            //设置组件类型。
            var compositionType = IFCValueObject.Create("ELEMENT", "IFCELEMENTCOMPOSITIONENUM");
            attributes.Add(compositionType);
            //设置参考高度，通常为0.0。
            var elevationOfRefHeight = IFCValueObject.Create(0.0, "IFCLENGTHMEASURE");
            attributes.Add(elevationOfRefHeight);
            //设置建筑物周围最小地形水平的高程。
            var elevationOfTerrain = IFCValueObject.Create(0.0, "IFCLENGTHMEASURE");
            attributes.Add(elevationOfTerrain);
            //设置邮编地址。
            attributes.Add(null);

            building.SetAttributes(attributes);
            return building;
        }

        /// <summary>
        /// 创建默认IFCBUILDINGSTOREY。
        /// https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/schema/ifcproductextension/lexical/ifcbuildingstorey.htm
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity CreateIfcBuildingStorey(IFCBuildingModel model)
        {
            var buildingStorey = model.CreateEntity("IFCBUILDINGSTOREY");

            List<IFCObject> attributes = new List<IFCObject>();
            //设置全局唯一标识符。
            var globallyUniqueId = IFCValueObject.CreateIfcGlobalUniqueId();
            attributes.Add(globallyUniqueId);
            //设置所有者信息。
            attributes.Add(IFCOwnerHistory);
            //设置名称。  
            attributes.Add(null);
            //设置描述。  
            attributes.Add(null);
            //设置类型。
            var objectType = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(objectType);
            //设置定位。
            var objectPlacement = model.CreateEntity("IFCLOCALPLACEMENT");
            List<double> origin = new List<double>() { 0, 0, 0 };
            Vector3d normal = new Vector3d(0, 0, 1);
            Vector3d direction = new Vector3d(1, 0, 0);
            var placement = CreateIfcAxis2Placement3D(origin, normal, direction, model);
            IFCObject nullObject = null;
            objectPlacement.SetAttributes(new List<IFCObject>() { nullObject, placement });
            attributes.Add(objectPlacement);
            //设置形体。
            attributes.Add(null);
            //设置长名称。
            var longName = IFCValueObject.Create("", "IFCLABEL");
            attributes.Add(longName);
            //设置组件类型。
            var compositionType = IFCValueObject.Create("ELEMENT", "IFCELEMENTCOMPOSITIONENUM");
            attributes.Add(compositionType);
            //设置标高。
            attributes.Add(null);

            buildingStorey.SetAttributes(attributes);
            return buildingStorey;
        }

        /// <summary>
        /// 创建IFCRELAGGREGATES。
        /// https://standards.buildingsmart.org/IFC/RELEASE/IFC4_1/FINAL/HTML/schema/ifckernel/lexical/ifcrelaggregates.htm
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <param name="ifcEntity">IFC实体对象。</param>
        /// <param name="ifcObjects">IFC对象集合。</param>
        private static void CreateIfcRelAggregation(IFCBuildingModel model, IFCEntity ifcEntity, List<IFCObject> ifcObjects)
        {
            var aggregates = model.CreateEntity("IFCRELAGGREGATES");

            List<IFCObject> attributes = new List<IFCObject>();
            //设置全局唯一标识符。
            var uniqueId = IFCValueObject.CreateIfcGlobalUniqueId();
            attributes.Add(uniqueId);
            //设置所有者信息。        
            attributes.Add(IFCOwnerHistory);
            //设置名称。
            var name = IFCValueObject.Create("CustomName", "IFCLABEL");
            attributes.Add(name);
            //设置描述。
            var description = IFCValueObject.Create("CustomDescription", "IFCTEXT");
            attributes.Add(description);
            attributes.Add(ifcEntity);

            var aggregation = IFCAggregation.Create(ifcObjects);
            attributes.Add(aggregation);

            aggregates.SetAttributes(attributes);
        }

        /// <summary>
        /// 创建坐标轴。
        /// https://standards.buildingsmart.org/IFC/RELEASE/IFC2x/FINAL/HTML/ifcgeometryresource/lexical/ifcaxis2placement3d.html
        /// </summary>
        /// <param name="origin">位置点</param>
        /// <param name="normal">平面法向</param>
        /// <param name="direction">轴网方向</param>
        /// <param name="model">IFC数据模型</param>
        /// <returns>IFC实体对象</returns>
        public static IFCEntity CreateIfcAxis2Placement3D(List<double> origin, Vector3d normal, Vector3d direction, IFCBuildingModel model)
        {
            var ifcAxis2Placement3D = model.CreateEntity("IFCAXIS2PLACEMENT3D");
            List<IFCObject> attributes = new List<IFCObject>();

            var ifcCoordinate = CreateIfcCartesianPoint(origin, model);
            attributes.Add(ifcCoordinate);
            var ifcNormal = CreateIfcDirection(normal, model);
            attributes.Add(ifcNormal);
            var ifcDirection = CreateIfcDirection(direction, model);
            attributes.Add(ifcDirection);

            ifcAxis2Placement3D.SetAttributes(attributes);
            return ifcAxis2Placement3D;
        }

        /// <summary>
        /// 创建笛卡尔坐标。
        /// https://standards.buildingsmart.org/IFC/RELEASE/IFC4_1/FINAL/HTML/schema/ifcgeometryresource/lexical/ifccartesianpoint.htm
        /// </summary>
        /// <param name="points">点集。</param>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity CreateIfcCartesianPoint(List<double> points, IFCBuildingModel model)
        {
            var ifcCartesianPoint = model.CreateEntity("IFCCARTESIANPOINT");

            List<IFCObject> coordinates = new List<IFCObject>();
            foreach (var point in points)
            {
                var coordinate = IFCValueObject.Create(point, "IFCLENGTHMEASURE");
                coordinates.Add(coordinate);
            }
            var attributes = IFCAggregation.Create(coordinates);
            List<IFCObject> attributeObjects = new List<IFCObject>
            {
                attributes
            };
            ifcCartesianPoint.SetAttributes(attributeObjects);
            return ifcCartesianPoint;
        }

        /// <summary>
        /// 创建IFC方向。
        /// https://standards.buildingsmart.org/IFC/RELEASE/IFC4_1/FINAL/HTML/schema/ifcgeometryresource/lexical/ifcdirection.htm
        /// </summary>
        /// <param name="direction">方向。</param>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity CreateIfcDirection(Vector3d direction, IFCBuildingModel model)
        {
            var ifcDirection = model.CreateEntity("IFCDIRECTION");
            List<IFCObject> ifcValues = new List<IFCObject>();

            var ifcDirX = IFCValueObject.Create(direction.X, "IFCREAL");
            ifcValues.Add(ifcDirX);
            var ifcDirY = IFCValueObject.Create(direction.Y, "IFCREAL");
            ifcValues.Add(ifcDirY);
            var ifcDirZ = IFCValueObject.Create(direction.Z, "IFCREAL");
            ifcValues.Add(ifcDirZ);

            var attributes = IFCAggregation.Create(ifcValues);
            List<IFCObject> objects = new List<IFCObject>
            {
                attributes
            };
            ifcDirection.SetAttributes(objects);
            return ifcDirection;
        }

        /// <summary>
        /// 创建IFCGEOMETRICREPRESENTATIONSUBCONTEXT。
        /// https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/schema/ifcrepresentationresource/lexical/ifcgeometricrepresentationsubcontext.htm
        /// </summary>
        /// <param name="parentContext">几何上下文。</param>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity ExportIfcGeometricRepresentationSubContext(IFCEntity parentContext, IFCBuildingModel model)
        {
            var subContext = model.CreateEntity("IFCGEOMETRICREPRESENTATIONSUBCONTEXT");

            List<IFCObject> attributes = new List<IFCObject>();
            //设置标识符。
            var identifier = IFCValueObject.Create("Body", "IFCLABEL");
            attributes.Add(identifier);
            //设置类型。
            var contextType = IFCValueObject.Create("Model", "IFCLABEL");
            attributes.Add(contextType);
            //设置维度。
            attributes.Add(null);
            //设置精度。
            attributes.Add(null);
            //设置世界坐标系。
            attributes.Add(null);
            //设置正北。
            attributes.Add(null);
            //设置父级上下文。
            attributes.Add(parentContext);
            //设置缩放比例。
            var targetScale = IFCValueObject.Create(1.0, "IFCPOSITIVERATIOMEASURE");
            attributes.Add(targetScale);
            //设置视图。
            var targetView = IFCValueObject.Create("MODEL_VIEW", "IFCGEOMETRICPROJECTIONENUM");
            attributes.Add(targetView);
            //设置用户定义视图。
            var userDefinedTargetView = IFCValueObject.Create("Model", "IFCLABEL");
            attributes.Add(userDefinedTargetView);

            subContext.SetAttributes(attributes);
            return subContext;
        }

        /// <summary>
        /// 设置IFCPROJECT。
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <param name="project">IFC项目。</param>
        /// <param name="context">几何上下文信息。</param>
        private static void SetIfcProjectConfig(IFCBuildingModel model, IFCEntity project, IFCEntity context)
        {
            List<IFCObject> attributes = new List<IFCObject>();
            //设置全局唯一标识符。
            var uniqueId = IFCValueObject.CreateIfcGlobalUniqueId();
            attributes.Add(uniqueId);
            //设置所有者信息。
            attributes.Add(IFCOwnerHistory);
            //设置名称。       
            attributes.Add(null);
            //设置描述。
            attributes.Add(null);
            //设置类型。
            attributes.Add(null);
            //设置长名称。
            attributes.Add(null);
            //设置阶段。
            attributes.Add(null);
            List<IFCObject> contextObject = new List<IFCObject>
            {
                context
            };
            var aggregation = IFCAggregation.Create(contextObject);
            attributes.Add(aggregation);
            //设置单位。
            var ifcUnitAssignment = SetUnitAssignment(model);
            attributes.Add(ifcUnitAssignment);

            project.SetAttributes(attributes);
        }

        /// <summary>
        /// 设置单位信息。
        /// https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/schema/ifcmeasureresource/lexical/ifcunitassignment.htm
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity SetUnitAssignment(IFCBuildingModel model)
        {
            var unitAssignment = model.CreateEntity("IFCUNITASSIGNMENT");

            List<IFCObject> attributes = new List<IFCObject>();
            //设置长度单位。
            var lengthUnit = ExportIfcSIUnit(model, "LENGTHUNIT", "METRE", "MILLI");
            attributes.Add(lengthUnit);
            //设置面积单位。
            var areaUnit = ExportIfcSIUnit(model, "AREAUNIT", "SQUARE_METRE", "MILLI");
            attributes.Add(areaUnit);
            //设置体积单位。
            var volumeUnit = ExportIfcSIUnit(model, "VOLUMEUNIT", "CUBIC_METRE", "MILLI");
            attributes.Add(volumeUnit);
            //设置体量单位。
            var massUnit = ExportIfcSIUnit(model, "MASSUNIT", "GRAM", "KILO");
            attributes.Add(massUnit);
            //设置时间单位。
            var timeUnit = ExportIfcSIUnit(model, "TIMEUNIT", "SECOND", null);
            attributes.Add(timeUnit);
            //设置平面角度单位。
            var planeAngleUnit = ExportIfcSIUnit(model, "PLANEANGLEUNIT", "RADIAN", null);
            attributes.Add(planeAngleUnit);
            //设置实体角度单位。
            var solidAngleUnit = ExportIfcSIUnit(model, "SOLIDANGLEUNIT", "STERADIAN", null);
            attributes.Add(solidAngleUnit);
            //设置温度单位。
            var thermoDynamicTemperatureUnit = ExportIfcSIUnit(model, "THERMODYNAMICTEMPERATUREUNIT", "DEGREE_CELSIUS", null);
            attributes.Add(thermoDynamicTemperatureUnit);
            //设置发光强度单位。
            var luminousIntensityUnit = ExportIfcSIUnit(model, "LUMINOUSINTENSITYUNIT", "LUMEN", "MILLI");
            attributes.Add(luminousIntensityUnit);

            var aggregation = IFCAggregation.Create(attributes);
            List<IFCObject> objects = new List<IFCObject>
            {
                aggregation
            };
            unitAssignment.SetAttributes(objects);

            return unitAssignment;
        }

        /// <summary>
        /// 创建SI单位。
        /// https://standards.buildingsmart.org/IFC/RELEASE/IFC4/ADD1/HTML/schema/ifcmeasureresource/lexical/ifcsiunit.htm
        /// </summary>
        /// <param name="model">IFC数据模型。</param>
        /// <param name="unitType">单位类型。</param>
        /// <param name="name">名称。</param>
        /// <param name="prefix">前缀。</param>
        /// <returns>IFC实体对象。</returns>
        private static IFCEntity ExportIfcSIUnit(IFCBuildingModel model, string unitType, string name, string prefix)
        {
            var unit = model.CreateEntity("IFCSIUNIT");

            List<IFCObject> attributes = new List<IFCObject>();
            attributes.Add(null);
            //设置单位类型。
            var ifcUnitType = IFCValueObject.Create(unitType, "IFCUNITENUM");
            attributes.Add(ifcUnitType);
            //设置前缀。
            if (!string.IsNullOrEmpty(prefix))
            {
                var ifcPrefix = IFCValueObject.Create(prefix, "IFCSIPREFIX");
                attributes.Add(ifcPrefix);
            }
            else
            {
                attributes.Add(null);
            } 
            //设置名称。
            var nameEntity = IFCValueObject.Create(name, "IFCSIUNITNAME");
            attributes.Add(nameEntity);
            unit.SetAttributes(attributes);
            return unit;
        }
    }
}
